package com.reshare.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.reshare.base.BaseService;
import com.reshare.bean.Permission;
import com.reshare.bean.Role;
import com.reshare.bean.UserRole;
import com.reshare.mapper.ModuleMapper;
import com.reshare.mapper.PermissionMapper;
import com.reshare.mapper.RoleMapper;
import com.reshare.query.RoleQuery;
import com.reshare.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class RoleService extends BaseService<Role, Integer> {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    private ModuleMapper moduleMapper;

    public Map<String, Object> SelectRoleList(RoleQuery roleQuery) {
        //查询
        List<Role> rlist = roleMapper.selectByParams(roleQuery);
        //实例化Map
        Map<String, Object> map = new HashMap<>();
        //准备列表展示的数据
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", rlist.size());
        map.put("data", rlist);
        //返回目标数据
        return map;
    }

    //添加方法
    public void saveRole(Role role) {
        //判断添加框是否为空
        AssertUtil.isTrue(StringUtils.isBlank(role.getRoleName()), "请填写添加的角色");
        //判断添加的角色是否存在
        Role temp = roleMapper.selectRoleByName(role.getRoleName());
        AssertUtil.isTrue(temp != null, "用户名已存在");
        role.setCreateDate(new Date());
        role.setUpdateDate(new Date());
        role.setIsValid(1);
        //插入
        AssertUtil.isTrue(roleMapper.insertSelective(role) < 1, "插入失败");
    }

    //修改方法
    public void update(Role role) {
        AssertUtil.isTrue(role.getId() == null || roleMapper.selectByPrimaryKey(role.getId()) == null, "用户名存在");
        //判断添加的角色是否存在
        Role temp = roleMapper.selectRoleByName(role.getRoleName());
        AssertUtil.isTrue(temp == null, "用户名不存在");

        role.setUpdateDate(new Date());
        role.setIsValid(1);
        //插入
        AssertUtil.isTrue(roleMapper.updateByPrimaryKeySelective(role) < 1, "修改失败");
    }

    public void deleteRole(Integer roleId) {
        //待删除的记录是否存在
        System.out.println(roleId);
        Role temp = roleMapper.selectByPrimaryKey(roleId);
        AssertUtil.isTrue(temp == null, "待删除的记录不存在");
        //删除
        temp.setIsValid(0);
        //删除记录失败
        AssertUtil.isTrue(roleMapper.deleteByPrimaryKey(roleId) < 1, "删除记录失败");

    }

    public void addGrant(Integer roleId, String[] mids) {
        /**
         * 核⼼表-t_permission t_role(校验⻆⾊存在)
         * 如果⻆⾊存在原始权限 删除⻆⾊原始权限
         * 然后添加⻆ ⾊新的权限 批量添加权限记录到t_permission
         */
        Role temp = roleMapper.selectByPrimaryKey(roleId);
        AssertUtil.isTrue(temp==null||roleId==null,"带授权的角色不存在！");
        //资源存在
        if (mids!=null||mids.length>0){
            //角色原来是否有资源，
            //若有资源，新增一部分，
            //删除原来的资源，重新分配
            //统计角色拥有
            int count=permissionMapper.countModuleByRoleId(roleId);
            if (count>0){
                //删除原来的角色信息
                AssertUtil.isTrue(roleMapper.deleteModuleByRoleId(roleId)!=count,"操作失败");
            }
            List<Permission> plist=new ArrayList<Permission>();
            for (String mid:mids ) {
                //实例化目标对象
                Permission permission=new Permission();
                //初始化角色id
                permission.setRoleId(roleId);
                //初始化资源id
                permission.setModuleId(Integer.parseInt(mid));

                permission.setCreateDate(new Date());
                permission.setUpdateDate(new Date());
                //获取当前资源的权限码
                String optValue = moduleMapper.selectByPrimaryKey(Integer.parseInt(mid)).getOptValue();
                permission.setAclValue(optValue);
                //添加道容器plist
                plist.add(permission);

            }
            //批量添加
            permissionMapper.insertBatch(plist);
        }



    }

    /**
     * 查询所有角色
     * @author WangYueHu
     * @date 2021/9/2 21:57
     * @param userId
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    public List<Map<String,Object>> findRoles(Integer userId){

        return roleMapper.selRoles(userId);
    }

}
